#include "tcpserver.h"
#include "ui_tcpserver.h"
#include "mytcpserver.h"
#include <QByteArray>
#include <QDebug>
#include <QMessageBox>
#include <QHostAddress>
TcpServer::TcpServer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TcpServer)
{
    ui->setupUi(this);
    loadConfig();
    //进行监听
    MyTcpServer::getIntance().listen(QHostAddress(m_strIP),m_usPort);
}

TcpServer::~TcpServer()
{
    delete ui;
}

void TcpServer::loadConfig()
{
    QFile file(":/server.config");
    //open方法的返回值为bool，成功打开返回true，失败返回false
    if(file.open(QIODevice::ReadOnly)){
        QByteArray baData=file.readAll();
        QString strData=baData.toStdString().c_str();
        //替换字符串中的\r\n
        strData.replace("\r\n"," ");
        //通过空格进行分割
        QStringList strList= strData.split(" ");
        m_strIP=strList.at(0);
        m_usPort=strList.at(1).toUShort();
        qDebug()<<"IP:"<<m_strIP<<"port:"<<m_usPort;
        file.close();
    }
    else {
        QMessageBox::critical(this,"open config","open config 失败");
    }
}
